﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using RFIDStockManagement.Model.Public;
using RFIDStockManagement.Common.Public.SQLLite;
using RFIDStockManagement.Common.Public.Enums;
using RFIDStockManagement.Common.Public;

namespace IOReadWriter
{
    public class RfidWriter
    {

        private Thread m_WriterThread = null;
        private bool m_Run = false;
        private int m_iStart = 0;
        private IStockContainer m_Container = null;
        public static string DbPath = AppDomain.CurrentDomain.BaseDirectory + "\\RfidDataBase.db3";
        private EnumIOStockOperate m_EnumIOStockOperate = EnumIOStockOperate.InStock;

        public RfidWriter(IStockContainer container, EnumIOStockOperate enmOperate)
        {
            m_Container = container;
            m_EnumIOStockOperate = enmOperate;
        }

        public void StartWrite()
        {
            if (m_Run == false)
            {
                m_Run = true;
                m_WriterThread = new Thread(new ThreadStart(DoWork));
                m_WriterThread.Start();
            }
        }

        public void StopWrite()
        {
            m_Run = false;
            if (m_WriterThread != null)
            {
                m_WriterThread.Abort();
            }
            RfidCacheManager.ClearAllCache(m_EnumIOStockOperate);
        }

        private void DoWork()
        {
            while (m_Run)
            {
                try
                {
                    int iStart = m_iStart;
                    string[] lstResult = RfidCacheManager.ReadRfidDataInfo(iStart, out m_iStart, m_EnumIOStockOperate);
                    if (lstResult != null && lstResult.Length > 0)
                    {
                        List<RfidDataInfo> lstRfidDataInfo = new List<RfidDataInfo>();
                        foreach (string rfid in lstResult)
                        {
                            bool bSuccess = false;
                            long[] rfidConvert = SpeedwayPublicFunction.ConvertRfid(rfid, out bSuccess);
                            if (bSuccess)
                            {
                                RfidDataInfo rfidDataInfo = new RfidDataInfo(rfid, rfidConvert[0], rfidConvert[1], rfidConvert[2]);
                                lstRfidDataInfo.Add(rfidDataInfo);
                            }
                        }
                        if (lstRfidDataInfo.Count > 0)
                        {
                            if (m_EnumIOStockOperate == EnumIOStockOperate.InStock || m_EnumIOStockOperate == EnumIOStockOperate.OutStock)
                            {
                                string table = m_EnumIOStockOperate == EnumIOStockOperate.InStock ? "InStock" : "OutStock";
                                StringBuilder sb = new StringBuilder();
                                foreach (RfidDataInfo rfidDataInfo in lstRfidDataInfo)
                                {

                                    sb.AppendFormat(@"INSERT INTO {0}(FItemID, FUnitID, FRfid)
SELECT {1},{2},'{3}'
WHERE NOT EXISTS (SELECT 1 FROM {0} WHERE FRfid='{3}');", table, rfidDataInfo.ItemId, rfidDataInfo.UnitId, rfidDataInfo.Rfid);

                                }
                                SQLiteDBHelper dbHelper = new SQLiteDBHelper(DbPath);
                                dbHelper.ExecuteNonQuery(sb.ToString(), null);
                            }

                            m_Container.IsShowed = false;
                            m_Container.ShowRfidData(lstRfidDataInfo);
                            while (m_Container.IsShowed == false)
                            {
                                Thread.Sleep(5);
                            }
                        }
                    }
                }
                catch (Exception ex)
                { }
            }
        }
    }
}
